//
//  MyViewController.m
//  FinancialProduct
//
//  Created by Stella on 2017/8/24.
//  Copyright © 2017年 maq. All rights reserved.
//

#import "MyAssetsController.h"
#import "MyAccountCell.h"
#import "CKPuslichTool.h"
#import "HandleNetProduct.h"
#import "MyViewController.h"
#import "AssetListController.h"
#import "InfomationController.h"
#import "TransactionListController.h"
#import "UserModel.h"
#import "Header.h"
#import "SigningListController.h"
#import "QianyueReminderController.h"
#import "SVProgressHUD.h"

@interface MyViewController ()
@property (weak, nonatomic) IBOutlet UILabel *TotalMoney;
@property (weak, nonatomic) IBOutlet UIImageView *BgImage;
@property (weak, nonatomic) IBOutlet UIButton *ButtonRMB;
@property (weak, nonatomic) IBOutlet UIButton *buttonMY;
@property (weak, nonatomic) IBOutlet UITableView *myTable;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *HearderViewHeight;
@property (weak, nonatomic) IBOutlet UIView *noView;
@property (strong,nonatomic) NSArray *DataArray;
@property (strong,nonatomic) NSDictionary *accountInfo;
@end
static BOOL isReLoad=YES;
@implementation MyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"我的账户";

    // Do any additional setup after loading the view.
}
-(void)initMyView{
    self.noView.hidden=YES;
    if ([[UserModel getUser].Accountstatus intValue]==2) {
        self.noView.hidden=NO;
        return;
    }
    self.HearderViewHeight.constant=140;
    self.DataArray=@[@{@"image":@"qianyue",@"name":@"电子签约"},
                     @{@"image":@"zichan",@"name":@"资产总览"},
                     @{@"image":@"mingxi",@"name":@"资产明细"},
                     @{@"image":@"jiaoyi",@"name":@"交易明细"},
                     @{@"image":@"pilou",@"name":@"信息披露"}];

    [self getUserData];

     /////////////////////////////测试动画///////////////////////////////////////////

    
    
    
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
 
    
    if (isReLoad) {
        [self initMyView];
        
    }else{
        isReLoad=YES;
    }
    
   
}
- (IBAction)ChangeMoneyUnit:(UIButton *)sender {
    if (sender.tag==0) {
        self.BgImage.image=[UIImage imageNamed:@"qiehuan-1"];
        self.ButtonRMB.selected=YES;
        self.buttonMY.selected=NO;
    
        //总资产转换为人民币
        self.TotalMoney.text=self.accountInfo[@"CXAssetsAmountRMB"];
    }else{
        self.BgImage.image=[UIImage imageNamed:@"qiehuan-2"];
        self.ButtonRMB.selected=NO;
        self.buttonMY.selected=YES;
        //总资产转换为美元
        self.TotalMoney.text=self.accountInfo[@"CXAssetsAmountUSD"];
    }
    
    
    NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.myTable reloadRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationFade];
}

-(void)getUserData{
    
    UserModel *mode=[UserModel getUser];
    int mid=[mode.CustomerID intValue];
//    int mid=13117;
    [HandleNetProduct getCAAssetsforCustomerID:mid Success:^(NSDictionary *data) {
        
        /*
         *CXAssetsAmountRMB = 0;人民币存续资产
         *CXAssetsAmountUSD = 0;美元存续资产
         *ISMoreCurrency = 0;是否多币别，注意：1为多币别，手机显示的是我的账户币种切换，如果为0，显示的是我的账户，多币别和但币别资产总览页面是不一样，需要注意
         *
         */
        
        self.accountInfo=data;
        if ([data[@"ISMoreCurrency"] intValue]==0) {
            self.HearderViewHeight.constant=140;
        }else{
            self.HearderViewHeight.constant=181;
            self.BgImage.image=[UIImage imageNamed:@"qiehuan-1"];
            self.ButtonRMB.selected=YES;
            self.buttonMY.selected=NO;
            
        }
        NSLog(@"self.accountInfo %@",self.accountInfo);
        self.TotalMoney.text=data[@"CXAssetsAmountRMB"];
        //有新的预约单时才显示
        if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"showQY"] isEqualToString:@"YES"]) {
           
            if (([self.accountInfo[@"ISRMBNew"] isEqualToString:@"Yes"] && self.ButtonRMB.selected)||([self.accountInfo[@"ISUSDNew"] isEqualToString:@"Yes"] && self.buttonMY.selected)) {
                //如果有新的预约单显示提示页面
                QianyueReminderController *vc = (QianyueReminderController *)[CKPuslichTool getViewControllerFromStroyBoard:@"My" viewID:@"QianyueReminderController"];
                [vc setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
                [vc setModalPresentationStyle:UIModalPresentationOverFullScreen];
                vc.returnBlock = ^{
                    [self goSigningListController];
                };
                
                [self presentViewController:vc animated:YES completion:nil];
                
            }
            [[NSUserDefaults standardUserDefaults]setObject:@"NO" forKey:@"showQY"];
        }
        
        
       
      
         int  num = [self.accountInfo[@"RMBSigningNum"] intValue] + [self.accountInfo[@"USDSigningNum"] intValue];
        
        
        NSDictionary *dic=@{@"num":[NSString stringWithFormat:@"%d",num]};
        [[NSNotificationCenter defaultCenter]postNotificationName:@"changeNumLabelText" object:nil userInfo:dic];
        [self.myTable reloadData];
    } Failure:^(NSString *errorInfo) {
        
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Table view data source
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50
    ;
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 1;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.DataArray.count;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    MyAccountCell *cell=[tableView dequeueReusableCellWithIdentifier:@"MyAccountCell" forIndexPath:indexPath];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    cell.accessoryType=UITableViewCellAccessoryNone;
    NSDictionary *info=self.DataArray[indexPath.row];
    cell.mImage.image=[UIImage imageNamed:info[@"image"]];
    cell.NameLabel.text=info[@"name"];
    cell.tagNew.hidden = YES;
    cell.numLabel.hidden = YES;
    if (indexPath.row == 0) {
        //有新的预约单时才显示
        if ([self.accountInfo[@"IsNew"] isEqualToString:@"Yes"]) {
            cell.tagNew.hidden = NO;

        }
        NSString *num;
        if (self.ButtonRMB.selected) {
            num = self.accountInfo[@"RMBSigningNum"];
        }else{
            num = self.accountInfo[@"USDSigningNum"];
        }
        if ([num intValue]> 0) {
            cell.tagNew.hidden = YES;
            cell.numLabel.hidden = NO;
            cell.numLabel.text = num;
        }
        
        
    }
    
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

-(void)goSigningListController{
    isReLoad=YES;
    SigningListController *vc = (SigningListController *)[CKPuslichTool getViewControllerFromStroyBoard:@"My" viewID:@"SigningListController"];
    if (self.ButtonRMB.selected) {
        vc.currency = @"RMB";
    }else{
        vc.currency = @"USD";
    }
    [self.navigationController pushViewController:vc animated:YES];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    isReLoad=NO;
    switch (indexPath.row) {
        case 0:
        {
            //电子签约
            [self goSigningListController];
            
        }
            break;
        case 1:
        {
            //资产总览
            MyAssetsController *vc=(MyAssetsController *)[CKPuslichTool getViewControllerFromStroyBoard:@"My" viewID:@"MyAssetsController"];
            if (self.ButtonRMB.selected) {
                vc.typeStr=@"RMB";
            }else{
                vc.typeStr=@"USD";
            }
            if ([self.accountInfo[@"ISMoreCurrency"] intValue]==0) {
                vc.isOnlyOne=@"ONE";
            }else{
                vc.isOnlyOne=@"MORE";
            }
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 2:
        {
            //资产明细
            AssetListController *vc=(AssetListController *)[CKPuslichTool getViewControllerFromStroyBoard:@"My" viewID:@"AssetListController"];
            if (self.ButtonRMB.selected) {
                vc.typeStr=@"RMB";
            }else{
                vc.typeStr=@"USD";
            }
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 3:
        {
            //交易明细
            TransactionListController *vc=(TransactionListController *)[CKPuslichTool getViewControllerFromStroyBoard:@"My" viewID:@"TransactionListController"];
            if (self.ButtonRMB.selected) {
                vc.typeStr=@"RMB";
            }else{
                vc.typeStr=@"USD";
            }
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 4:
        {
            //信息披露
            InfomationController *vc=(InfomationController *)[CKPuslichTool getViewControllerFromStroyBoard:@"My" viewID:@"InfomationController"];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
            
        default:
            break;
    }
   
}

@end
